package com.xilihui.xlh.business.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.GoodDetailsEntity;
import com.xilihui.xlh.business.entities.SpecEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ScreenUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/5/30.
 */

public class MarqueDialog {
    
    private Activity activity;
    private Dialog dialog;
    private SimpleDraweeView simpleDraweeView;
    private TextView tv_price;
    private TextView tv_kucuei;
    private ImageView iv_close;
    private TextView tv_queding;

    GoodDetailsEntity detailsEntity;
    
    String id = "";
    String goods_id = "";
    String agent_id = "";
    String item_id = "";
    String num = "";
    String wa_id = "";
    

    ArrayList<GoodDetailsEntity.FilterSpecBean> data;
    String[] select;
    
    public MarqueDialog(Activity activity,GoodDetailsEntity detailsEntity,String id,String goods_id,String agent_id,String num,String wa_id){
        this.activity = activity;
        this.detailsEntity = detailsEntity;
        this.id = id;
        this.goods_id = goods_id;
        this.agent_id = agent_id;
        this.item_id = detailsEntity.getItem_id();
        this.num = num;
        this.wa_id = wa_id;
        dialog = new Dialog(this.activity, R.style.Dialog);
        dialog.setContentView(R.layout.dialog_marque);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        setDialogWidth();
        initView();
    }
    
    public void setDialogWidth(){
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = ScreenUtil.getScreenWidth(activity);
        dialog.getWindow().setAttributes(lp);
    }
    
    public void initView(){

        final RecyclerView recyclerView = (RecyclerView)dialog.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        simpleDraweeView = (SimpleDraweeView)dialog.findViewById(R.id.sdv_img);
        tv_price = (TextView)dialog.findViewById(R.id.tv_price);
        tv_kucuei = (TextView)dialog.findViewById(R.id.tv_kuchuen);
        iv_close = (ImageView)dialog.findViewById(R.id.iv_close);
        tv_queding = (TextView)dialog.findViewById(R.id.tv_qingdan);
        
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        
        //确定
        tv_queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyAppointmentRequest.updateCart(activity,id,agent_id,item_id,num,wa_id)
                                    .compose(DoTransform.<BaseEntity>applyScheduler((FragmentActivity) activity,true))
                                    .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                        @Override
                                        protected void onSuccess(BaseEntity entity) {
                                            ToastUtil.toastLongPositive(entity.getMsg());
                                            dialog.dismiss();
                                            YEventBuses.post(new YEventBuses.Event("cartupdate"));
                                        }

                                        @Override
                                        protected void onFail(String msg) {
                                            ToastUtil.toastShortNegative(msg);
                                        }
                                    });
            }
        });
        
        simpleDraweeView.setImageURI(UrlConst.baseUrl()+detailsEntity.getGoods().getOriginal_img());
        tv_price.setText("￥"+detailsEntity.getGoods().getShop_price());
        tv_kucuei.setText("库存量"+detailsEntity.getGoods().getStore_count()+"件");
        
        data = (ArrayList<GoodDetailsEntity.FilterSpecBean>) detailsEntity.getFilter_spec();
        select = new String[data.size()];
        
        BaseAdapter<GoodDetailsEntity.FilterSpecBean> baseAdapter = new BaseAdapter<GoodDetailsEntity.FilterSpecBean>(activity,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_marque;
            }

            @Override
            protected void getView(BaseViewHolder holder, GoodDetailsEntity.FilterSpecBean entity, final int pos) {

                holder.setText(R.id.tv_shuxing, entity.getName());

                RecyclerView recyclerView1 = (RecyclerView) holder.getView(R.id.recyclerview);
                recyclerView1.setLayoutManager(new LinearLayoutManager(activity, LinearLayout.HORIZONTAL,
                        false));

                final ArrayList<GoodDetailsEntity.FilterSpecBean.SpecBean> specBeans = (ArrayList<GoodDetailsEntity.FilterSpecBean.SpecBean>) entity.getSpec();
                

                BaseAdapter<GoodDetailsEntity.FilterSpecBean.SpecBean> baseAdapter1 =
                        new BaseAdapter<GoodDetailsEntity.FilterSpecBean.SpecBean>(activity, specBeans) {
                            @Override
                            protected int setLayoutId() {
                                return R.layout.recycler_item_good_model;
                            }

                            @Override
                            protected void getView(BaseViewHolder holder, final GoodDetailsEntity.FilterSpecBean.SpecBean entity, final int position) {
                                holder.setText(R.id.tv, entity.getItem());
                                if (entity.getSelect().equals("1")) {
                                    holder.setSelected(R.id.tv, true);
                                    select[pos] = specBeans.get(position).getItem_id();
                                } else {
                                    holder.setSelected(R.id.tv, false);
                                }

                                holder.itemView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        for (int i = 0; i < specBeans.size(); i++) {
                                            if (i == position) {
                                                specBeans.get(i).setSelect("1");
                                                select[pos] = specBeans.get(position).getItem_id();

                                                String key = "";
                                                for (int j = 0; j < select.length; j++) {
                                                    key = key + select[j] + "_";
                                                }
                                                if (!TextUtil.isEmpty(key)) {
                                                    key = key.substring(0, key.length() - 1);
                                                }
                                                
                                                MyAppointmentRequest.spce(key,goods_id)
                                                        .compose(DoTransform.<SpecEntity>applyScheduler((FragmentActivity) activity, true))
                                                        .subscribe(new HttpSubscriber<SpecEntity>(activity) {
                                                            @Override
                                                            protected void onSuccess(SpecEntity entity) {
                                                                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getSpec_goods().getSpec_img());
                                                                tv_price.setText("￥"+entity.getSpec_goods().getPrice());
                                                                tv_kucuei.setText("库存量"+entity.getSpec_goods().getStore_count()+"库");
                                                                item_id = entity.getItem_id();
                                                            }

                                                            @Override
                                                            protected void onFail(String msg) {
                                                                ToastUtil.toastShortNegative(msg);
                                                            }
                                                        });

                                            } else {
                                                specBeans.get(i).setSelect("0");
                                            }
                                        }
                                        notifyDataSetChanged();
                                    }
                                });

                            }
                        };
                baseAdapter1.setAnimationsLocked(true);
                recyclerView1.setAdapter(baseAdapter1);
            }
        };
        
        baseAdapter.setAnimationsLocked(true);
        recyclerView.setAdapter(baseAdapter);
        
        
    }
    
    public void show(){
        dialog.show();
    }
    
}
